package com.myapplication.ui.infofragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.myapplication.R;
import com.myapplication.bean.ZiXunBean;
import com.myapplication.config.HUrlConfig;
import com.myapplication.ui.infofragment.infowebactivity.InfoWebActivity;
import com.myapplication.utils.MarqueeTextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zitaojiang on 2016/10/6.
 */
public class Fragment_Info extends Fragment implements Contract_Info.View{
    private Contract_Info.Presenter mPresenter;
    private PullToRefreshListView lv_zixun;
    private ListView mlistview;
    private ZiXunfragmentAdapter adapter;
    private int page=0;
    private int LASTSHOWID=0;
    private Map<String,String> params=new HashMap<>();
    private Map<String,String> params2=new HashMap<>();
    private List<ZiXunBean.DataBean.ListBean> zixunBeanList=new ArrayList<>();
    private MarqueeTextView tv_zixun_marquee;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter=new Presenter_Info(this);
        params.put(HUrlConfig.key.PAGE, String.valueOf(page));
        params.put(HUrlConfig.key.LASTSHOWID, String.valueOf(HUrlConfig.DefaultValue.LASTSHOWID));
        params.put(HUrlConfig.key.APP_VERSION, HUrlConfig.DefaultValue.APP_VERSION);
        params.put(HUrlConfig.key.PLATFORM, HUrlConfig.DefaultValue.PLATFORM);
        mPresenter.GetData(params);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_info,null);
        lv_zixun= (PullToRefreshListView) view.findViewById(R.id.lv_zx);
        tv_zixun_marquee= (MarqueeTextView) view.findViewById(R.id.tv_zixun_marquee);
        mlistview=lv_zixun.getRefreshableView();
        lv_zixun.setMode(PullToRefreshBase.Mode.BOTH);
        initMarquee();
        initEvent();
        return view;
    }

    private void initMarquee() {
//        String str="http://app.huihui.cn/app/zixun/list?page=0&lastshowid=0&app_version=3.9&platform=android&device_id=863777026126135&model=Coolpad+8297&vendor=myapp&appname=deals_app&system_version=4.2.2";
        String str= getActivity().getResources().getString(R.string.about);
////        CharSequence charSequence= Html.fromHtml(str);
        tv_zixun_marquee.setText(str);
//        tv_zixun_marquee.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private void initEvent() {
        lv_zixun.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mPresenter.GetData(params);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                //获取一个新的URL
                //加载新的一页数据 获得新params02
                //params.addAll(params02);
                //adapter.notifiedSetChanged();
                LASTSHOWID=zixunBeanList.get(zixunBeanList.size()-1).getHuiZixun().getId();
                params2.put(HUrlConfig.key.LASTSHOWID, String.valueOf(LASTSHOWID));
                params2.put(HUrlConfig.key.PAGE,String.valueOf(page));
                mPresenter.GetMoreData(params2);

            }
        });

        lv_zixun.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(getActivity(), InfoWebActivity.class);
                intent.putExtra("title",zixunBeanList.get(i-1).getHuiZixun().getTitle());
                intent.putExtra("url",zixunBeanList.get(i-1).getHuiZixun().getUrl());
                startActivity(intent);
            }
        });
    }


    @Override
    public void onGetDataSuccess(final List<ZiXunBean.DataBean.ListBean> zixunBeanList) {
        if(page==0){
            this.zixunBeanList.clear();
            this.zixunBeanList.addAll(zixunBeanList);
            adapter=new ZiXunfragmentAdapter(getActivity(),zixunBeanList);
            adapter.setZiXunBeanList(this.zixunBeanList);
            mlistview.setAdapter(adapter);
        }else{
            this.zixunBeanList.addAll(zixunBeanList);
            adapter.setZiXunBeanList(this.zixunBeanList);
            adapter.notifyDataSetChanged();
        }

        lv_zixun.onRefreshComplete();
    }

    @Override
    public void onGetDataFail(String error) {
        Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
    }
}
